package tryjson;

import java.lang.reflect.Field;
import java.util.ArrayList;

public class WriteJson {
    public static void main(String[] args) {
        // 创建对象
//        Student stu = new Student("张三", 19, 90, 100);
//        String s = oneWriteJson(stu);
//        System.out.println(s);

        Student[] stus = new Student[4];
        stus[0] = new Student("张三", 19, 90, 100);
        stus[1] = new Student("张三", 19, 90, 100);
        stus[2] = new Student("张三", 19, 90, 100);
        stus[3] = new Student("张三", 19, 90, 100);
        System.out.println(arrayWriteJson(stus));//数组转json字符串
        ArrayList<Object> list = new ArrayList<>();
        for (Student student : stus) {
            list.add(student);
        }
        System.out.println(arrayWriteJson(list));//集合转json字符串
    }
    //将单个JSON格式的对象字符串 转换为 java对象 以Student为模板
    //例如：[{"name":"张三","age":19,"score":90,"id":100}]
    //{name='张三', age=19, score=90.0, id=100}
    public static <T> T arrayWriteJson(String json,Class<T> clazz) {
        if (json == null){
            return null;
        }
        if (json.startsWith("[") && json.endsWith("]")){
            json = json.substring(1,json.length()-1);//去掉方括号
        }
        String[] split = json.split("},");//拆分json字符串
        //对每个值取出
        for (String s : split) {
            String[] kv = s.split(",");
            for (String kv1 : kv) {
                String[] kv2 = kv1.split(":");
                String key = kv2[0];
                String value = kv2[1];
                //将value中的双引号去掉
                value = value.substring(1,value.length()-1);
                //将value中的单引号去掉
                value = value.replace("'", "");
                //将value中的true替换为true
                value = value.replace("true", "true");
            }
        }

        //创建StringBuilder对象,用于拼接json字符串
        return null;
    }


    // 将java中的集合对象转为json格式的字符串

    public static <T> String arrayWriteJson(ArrayList<T> list) {
        if (list == null){
            return "[]";
        }
        return arrayWriteJson(list.toArray());
    }

    // 手写 数组转json字符串数组
    public static <T> String arrayWriteJson(T[] array) {
        if (array == null){
            return "[]";
        }
        //创建StringBuilder对象,用于拼接json字符串
        StringBuilder json = new StringBuilder();
        json.append("[");//拼接json字符串的开头
        for (T t : array) {
            json.append(oneWriteJson(t));
            json.append(",");
        }
        json.deleteCharAt(json.length()-1);//去掉最后一个逗号
        json.append("]"); //拼接json字符串的结尾

        return json.toString();
    }

    // 将单个java对象转json字符串
    public static <T> String oneWriteJson(T t) {
        try {
            if (t == null){//判断对象是否为null
                return "{}";
            }
            //创建StringBuilder对象,用于拼接json字符串
            StringBuilder json = new StringBuilder();

            //获取类对象
            Class clazz = t.getClass();
            json.append("{");//拼接json字符串的开头
            //获取所有属性
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                //获取属性名称
                String name = declaredField.getName();

                if (name.equals("serialVersionUID")){//判断属性名称是否为serialVersionUID
                    continue;
                }
                //拼接属性名称
                name = "\"" + name + "\"";
                declaredField.setAccessible(true);//暴力访问
                Object value = declaredField.get(t); //获取属性值

                //获取属性类型
                Class type = declaredField.getType();

                if (type.equals(StringBuffer.class)||type.equals(StringBuilder.class)||type.equals(String.class)){
                    value = "\"" + value + "\"";
                }
                json.append(name + ":" + value  +",");
            }
            json.deleteCharAt(json.length()-1);//去掉最后一个逗号
            json.append("}"); //拼接json字符串的结尾
            return json.toString();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
